import { ui } from "./ui/layaMaxUI";
import GameWx from "./GameWx";
import GameEvent from "./GameEvent";
import WxRomminfo from "./WxObje/WxRoomInfo";
import MaoRandom from "./MaoRandom";

export default class MyUi extends ui.HomeUI{
    constructor(){
        super();
        this.btn0.on(Laya.Event.CLICK,this,this.btn0click);
        this.btn1.on(Laya.Event.CLICK,this,this.btn1click);
        this.btn2.on(Laya.Event.CLICK,this,this.btn2click);
        this.p1.on(Laya.Event.CLICK,this,this.Shooting);
        this.p2.on(Laya.Event.CLICK,this,this.share);
        //GameWx.I.event( GameEvent.Event_WX_OnShow , [res.query]);
        this.update();
        this.label3.text = GameWx.I.key1;
        GameWx.I.on( GameEvent.Event_WX_OnShow , this, this.onshow);
        if(GameWx.I.key1!=""){
            this.btn0.label = "加入房间";
        }
    }

    onshow(rew:any){
        //console.log(" myui ohshow",GameWx.I.key1);        
        this.label3.text = GameWx.I.key1;
    }

    update(){
        this.btn0.visible = (this.isroom == 0);
        this.btn1.visible = (this.isroom == 2);
        this.btn2.visible = (this.isroom == 3);
        this.label0.visible = 
        this.label1.visible = 
        this.label2.visible = this.btn2.visible;
    }

    isroom = 0;
    share(){
        if(GameWx.I.accessInfo!=""){
            GameWx.I.shareAppMessage();
        }
    }

    private colors_arr = ['#ff0000','#00ff00','#0000ff'];
    Shooting(){
        if(this.isroom == 3){
            var gwx:GameWx = GameWx.I;
            gwx.uploadFrame([ this.colors_arr[Math.floor(Math.random()*3)] ]);
        }
    }
    
    btn0click(){
        this.isroom = 1;
        this.update();
        var gwx:GameWx = GameWx.I;

        gwx.on(GameEvent.Event_WX_Room_Success,this,this.onroom);
        if(gwx.key1!=""){
            gwx.joinRoom();
        }else{            
            gwx.getRoom();
        }        
	}

	onroom(res:any){
        var roominfo = res.data.roomInfo as WxRomminfo;
        //console.log("roominfo",roominfo);
        //console.log("roominfo.seed",roominfo.seed);
        if(roominfo.seed){                
            MaoRandom.seed = parseFloat( roominfo.seed );
        }else{
            MaoRandom.seed = 1966719;
        }

        this.isroom = 2;
        this.update();
		var gwx:GameWx = GameWx.I;
    }
    
    btn1click(){
        this.isroom = 3;
        this.update();
        var gwx:GameWx = GameWx.I;
        gwx.startGame();
    }

    btn2click(){
        this.isroom = 4;
        var gwx:GameWx = GameWx.I;
        gwx.on(GameEvent.Event_WX_Room_logout,this,this.logout);
        gwx.endGame();
    }

    private logout(){
        this.isroom = 0;
        this.update();
    }
}
